package com.vm.osm.business.service.eam.impl;

import com.github.pagehelper.StringUtil;
import com.vm.osm.business.dto.eam.manufactor.Manufactor;
import com.vm.osm.business.service.eam.IEamManufactorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 生产厂家服务实现类
 *
 * @author Xuerong Xue
 */
@Service
public class EamManufactorServiceImpl implements IEamManufactorService {
    private static final Logger LOG = LoggerFactory.getLogger(EamManufactorServiceImpl.class);
    @Value("${eam_api_url}")
    private String EAM_API_URL;
    @Autowired
    private RestTemplate restTemplate;


    @Override
    public List<Manufactor> getBySessionId(String sessionId) {
        try {
            LOG.info("【设备资产】（获取生产厂家列表）session_id={}", sessionId);
            if (StringUtil.isEmpty(sessionId)) return Collections.emptyList();
            String URL = EAM_API_URL + "/eam/v1.0/common/manufactors";
            HttpHeaders headers = new HttpHeaders();
            headers.add("session_id", sessionId);
            HttpEntity<Object> entity = new HttpEntity<>(null, headers);
            ResponseEntity<Manufactor[]> exchange =
                    restTemplate.exchange(URL, HttpMethod.GET, entity, Manufactor[].class);
            return Arrays.asList(exchange.getBody());
        } catch (HttpClientErrorException e) {
            LOG.error("【设备资产】（获取生产厂家列表）异常", e.getResponseBodyAsString());
            return null;
        } catch (Exception e) {
            LOG.error("【设备资产】（获取生产厂家列表）异常", e.getMessage());
            return null;
        }
    }
}
